<template>
  <div id="app">
    <el-container>
      <el-header>
        <div style="float:left;font-size:25px;margin-left:20px;margin-top:15px;color:#409eff">
          <i class="el-icon-s-home"></i>
          <el-button type="text" @click="$router.push('/',()=>{},()=>{})">返回首页</el-button>
        </div>
        <h1>消息页面</h1>
      </el-header>
      <el-container>
        <el-aside width="300px">
          <el-menu :default-active="activeIndex" @select="handleSelect">
            <el-menu-item
              v-for="(message, index) in messages"
              :key="index"
              :index="index.toString()"
            >
              <i class="el-icon-chat-line-square"></i>
              {{ message.title }}
              <span v-if="message.status === '未读'" class="red-dot"></span>
            </el-menu-item>
          </el-menu>
        </el-aside>
        <el-main>
          <div v-if="activeMessage">
            <h2>{{ activeMessage.title }}</h2>
            <h6>发信人：{{ activeMessage.sender }} | 消息类型： {{ activeMessage.messageType }} | 发送时间：{{ activeMessage.messageTime }}</h6>
            <p>{{ activeMessage.content }}</p>
            <a
              style="color:#409eff"
              v-if="activeMessage.messageType === '分配'"
              herf="#"
              @click="$router.push('/contract')"
            >点击签署合同</a>
          </div>
          <div v-else>
            <p>请选择一条消息</p>
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  data() {
    return {
      activeIndex: "0",
      messages: []
    };
  },
  computed: {
    activeMessage() {
      return this.messages[this.activeIndex];
    }
  },
  mounted() {
    this.getMessages();
  },
  methods: {
    handleSelect(index) {
      this.activeIndex = index;
      this.markAsRead(index);
    },
    getMessages() {
      if (this.$store.state.isLogin) {
        this.$axios
          .post("message/get", {
            tenant: this.$store.state.loginTenant.account,
            status: ""
          })
          .then(response => {
            this.messages = response.data.data;
          })
          .catch(error => {
            console.log(error);
          });
      } else {
        this.$message.error("请先登录");
        this.$router.push("/login");
      }
    },
    markAsRead(index) {
      if (this.messages[index].status === "未读") {
        this.messages[index].status = "已读";
        this.$axios
          .post("message/update", {
            id: this.messages[index].id,
            status: "已读"
          })
          .catch(error => {
            console.log(error);
          });
      }
    }
  }
};
</script>

<style scoped>
#app {
  height: 100vh;
}
.el-header {
  background-color: #f5f5f5;
  color: black;
  text-align: center;
  line-height: 22px;
}
.el-aside {
  background-color: #f2f2f2;
}
.el-menu {
  height: 92vh;
}
.el-main {
  padding: 20px;
}
.red-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  background-color: red;
  border-radius: 50%;
  margin-left: 5px;
}
</style>
